﻿// -----------------------------------------------------------------------
// <copyright file="ComparerFormatter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sitecore.ChangesTracker.UI
{
    using System;
    using System.Collections.Generic;
  using System.Linq;
    using System.Web.UI.WebControls;
  using Sitecore.ChangesTracker.Archiving;
    using Sitecore.Data.Items;
  using Sitecore.Diagnostics;
    using Sitecore.Web.UI.HtmlControls;
  using Sitecore.Web.UI.WebControls;

    public class ComparerFormatter : Sitecore.Shell.Applications.ContentEditor.EditorFormatter
  {
    private ArchivedItem arcItem;
    private ViewItemInfo controlPoint;

    public ComparerFormatter(ArchivedItem item, ViewItemInfo cp)
    {
      this.arcItem = item;
      this.controlPoint = cp;
    }

    public override void RenderField(System.Web.UI.Control parent, Shell.Applications.ContentManager.Editor.Field field, Item fieldType, bool readOnly)
    {
        this.Arguments.ShowInputBoxes = false;
      Assert.ArgumentNotNull(parent, "parent");
      Assert.ArgumentNotNull(field, "field");
      Assert.ArgumentNotNull(fieldType, "fieldType");
      
      GridPanel control = new GridPanel
      {
        Width = new Unit(100.0, UnitType.Percentage),
        Columns = 5
      };
      Context.ClientPage.AddControl(parent, control);
      Border border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "24");
      control.SetExtensibleProperty(border, "valign", "top");
      Space space = new Space("8", "1");
      border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "50%");
      control.SetExtensibleProperty(border, "valign", "top");
      base.RenderField(border, field, fieldType, true);
      space = new Space("8", "1");
      Context.ClientPage.AddControl(control, space);
      border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "50%");
      control.SetExtensibleProperty(border, "valign", "top");
      string arcValue = null;
      ArchivedField aField = null;
      
      if (field.TemplateField.IsShared)
      {
        aField = arcItem.Fields.FirstOrDefault(f => f.SharingType == "shared" && f.FieldId == field.TemplateField.ID.ToGuid());
      }
      else
      {
        if (field.TemplateField.IsUnversioned)
        {
          aField = arcItem.Fields.FirstOrDefault(f => f.SharingType == "unversioned" && f.FieldId == field.TemplateField.ID.ToGuid() && this.controlPoint.Lnaguage == f.Language);
        }
        else
        {
          List<ArchivedField> test = arcItem.Fields.Where(f => f.FieldId == field.TemplateField.ID.ToGuid() && f.SharingType == "versioned" && f.Language == this.controlPoint.Lnaguage).ToList();
          if (test.Count > 0)
          {

            int version = this.controlPoint.Version;
            if (version == 0)
            {

              version = test.Where(f => f.FieldId == field.TemplateField.ID.ToGuid()).Max(delegate(ArchivedField fld)
              {
                return fld.Version;
              });
            }

            aField = test.FirstOrDefault(f => this.controlPoint.Lnaguage == f.Language && version == f.Version);
          }
        }
      }

      if (aField != null)
      {
              arcValue = aField.Value;
      }
        Sitecore.Shell.Applications.ContentManager.Editor.Field clone = field.Clone(Control.GetUniqueID("RFIELD"));
        base.RenderField(border, clone, fieldType, true, arcValue ?? "[Standard value]");
        var c = border.FindControl(clone.ControlID).Parent;
        if (arcValue!=field.ItemField.GetValue(false,false))
            {

                System.Web.UI.WebControls.WebControl ctl = this.GetControl(c, 2);
                ctl.Style.Add("color", " Red");
                ctl.Style.Add("border", "1px solid Red");
                ctl.Attributes.Remove("disabled");
            }
        
    }

        private WebControl GetControl(System.Web.UI.Control start,int level)
        {
            for (int i = 0; i < level; ++i)
            {
                while (!(start is System.Web.UI.WebControls.WebControl))
                {
                    start = start.Parent;
                }
                if (i < level - 1)
                {
                    start = start.Parent;
                }
            }

            return start as WebControl;
        }
  }
}
